/**
 * Created by huhai on 17/9/6.
 */
require('babel-polyfill')
const puppeteer = require('puppeteer');
let gPDF = async (hospitalID = 120129,templateId,style) => {
  const browser = await puppeteer.launch({
    args:['--no-sandbox','--disable-setuid-sandbox'],
    dumpio:true
  });

  const page = await browser.newPage();
  const width = style ? 1080 : 769
  const height = style ? 769 : 1080
  page.setViewport({
    width: width,
    height: height
  })
  if(templateId == 4){
    console.log('截图模版4！！！！模版id：' +templateId)
      await page.goto(`http://121.42.252.26:3000/horizontal/${hospitalID}?template=${templateId}&style=${style}`)
  }else{
    console.log('截图模版1，2，3！！！模版id：' + templateId)
      await page.goto(`http://121.42.252.26:3000/${hospitalID}?template=${templateId}&style=${style}`)
  }
  // await page.goto(`http://www.baidu.com`)


   function screenshotPDF() {
    return new Promise((resolve)=>{
      setTimeout(()=>{
       // clip-pdf
        var random =Math.random()
        let name =`${hospitalID}的报告${random}.pdf`;
           page.pdf({
          path: name,
          width: width + "px",
          height: height + "px",
          printBackground:true
        }).then(()=>{
          // console.log('截图完成！',name)
          page.close().then(()=>{
            console.log('截图完成！',name)
            resolve(name)
          });

        })
       // clip-pdf
      },3000)
    })
  }

  return screenshotPDF()
}

module.exports={
  gPDF:gPDF
}